home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / XCSHELL / MAIN.C next >
C/C++ Source or Header  |  1991-08-31  |  3KB  |  127 lines

  1. //
  2. //    "XcShell" Skeletal XCMD/XFCN    housekeeping code
  3. //
  4. //    Copyright ⌐1991 Mark M. Owen -- All rights reserved
  5. //
  6.  
  7. #include <SetupA4.h>
  8. #include <SegLoad.h>
  9.  
  10. #include "XCMD.h"
  11.  
  12.  
  13. extern void        UnloadA4Seg        (void*aFunctionName);
  14.  
  15. pascal void        main            (XCmdBlockPtr paramPtr);
  16.  
  17.  
  18. pascal void    main(XCmdBlockPtr paramPtr)
  19. {
  20.     Handle        hSelf;
  21.     
  22.     //    Find out where this code is; lock it so it doesn't move while
  23.     //    its in use; set up register A4 for access to our "world"
  24.     
  25.     RememberA0();
  26.     SetUpA4();
  27.     asm {
  28.         _RecoverHandle;
  29.         move.l    A0,hSelf;
  30.     }
  31.     HLock( hSelf);
  32.     
  33.     {
  34.         register
  35.             hGlobals    **hG, hGlb;
  36.     
  37.         //    try to find an existing GLOBALTYPE resource named GLOBALS in the
  38.         //    current resource file.
  39.     
  40.         ResrvMem( sizeof(Handle) );
  41.         if( !(hG    = (hGlobals**)Get1NamedResource( GLOBALTYPE, GLOBALS )) ) {
  42.             int    resId;
  43.             
  44.             //    create a GLOBALSTYPE resource in which we will keep a
  45.             //    reference to a relocatable chunk of memory in which the
  46.             //    values of our globals will be kept between calls.  note
  47.             //    that the resource contains a handle to the actual globals
  48.             //    record in memory, written to disk (initially zero) so it
  49.             //    can be located easily when needed.
  50.             
  51.             **( hG = (hGlobals**)NewHandle( sizeof(Handle) ) ) = 0L;
  52.             do    resId = UniqueID( GLOBALTYPE );
  53.             while( resId < 128 );
  54.             AddResource( (Handle)hG, GLOBALTYPE, resId, GLOBALS );
  55.             WriteResource( (Handle)hG );
  56.             UpdateResFile( CurResFile() );
  57.         }
  58.         
  59.         HLock( (Handle)hG );    //    lock the global resource record
  60.         
  61.         if( !(hGlb = **hG) ) {
  62.             long    grow=5;
  63.             
  64.             MaxApplZone();        //    expand the callers heap zone if possible
  65.             
  66.             while( grow-- )
  67.                 MoreMasters();    //    allocate a few master pointer blocks
  68.                 
  69.             grow = MaxMem( &grow );
  70.             
  71.             //    allocate the relocatable space in which we will keep
  72.             //    the values of our globals will be kept between calls
  73.             //    and write the address out to disk so we can find it
  74.             //    next time we are called.  initial value is all zeros.
  75.             
  76.             ResrvMem( sizeof( globals ) );
  77.             hGlb = (hGlobals)NewHandleClear( sizeof(globals) );
  78.             HLock( (Handle)hGlb );    
  79.             **hG = hGlb;
  80.             WriteResource( (Handle)hG );
  81.             UpdateResFile( CurResFile() );
  82.             
  83.             //    load the related XCMD resources in to memory and
  84.             //    record their handles in our globals.  while we are
  85.             //    at it, we'll put them in high memory and lock them.
  86.             
  87.             (**hGlb).hVerbs        = Get1NamedResource( 'STR#', VERBS );
  88.             (**hGlb).hNParams    = Get1NamedResource( 'vprm', NPARAMS );
  89.             (**hGlb).nVerbs        = *((int*)(*((**hGlb).hVerbs)));
  90.             
  91.             MoveHHi( (**hGlb).hVerbs );
  92.             HLock( (**hGlb).hVerbs );
  93.             MoveHHi( (**hGlb).hNParams );
  94.             HLock( (**hGlb).hNParams );
  95.         }
  96.         
  97.         RestoreGlobals( *hGlb );    //    load global values saved after the last call
  98.         
  99.         //    run the XCMD code
  100.         
  101.         if( XCMD( paramPtr, hGlb ) == DEAD ) {
  102.         
  103.             //    dispose requested: time to terminate
  104.             
  105.             HUnlock( (**hGlb).hVerbs );
  106.             ReleaseResource( (**hGlb).hVerbs );
  107.             HUnlock( (**hGlb).hNParams );
  108.             ReleaseResource( (**hGlb).hNParams );
  109.             HUnlock( (Handle)hGlb );
  110.             DisposHandle( (Handle)hGlb );
  111.             **hG = 0L;
  112.             WriteResource( (Handle)hG );
  113.             UpdateResFile( CurResFile() );
  114.             ReleaseResource( (Handle)hG );
  115.             UnloadA4Seg( 0L );
  116.         }
  117.         else
  118.             SaveGlobals( *hGlb );    //    store global values for use during the next call
  119.     
  120.         HUnlock( (Handle)hG );        //    allow the globals to float around
  121.     }
  122.     
  123.     //    make ourselves relocatable again and restore register A4's contents
  124.     
  125.     HUnlock( hSelf );
  126.     RestoreA4();
  127. }